package com.controller;

import com.model.*;
import com.response.Response;
import com.service.*;
import com.util.PageBean;
import com.util.removeHTML;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/api/notices")
public class NoticesController{
	
	@Resource
	private NoticesService noticesService;
	
	//资讯列表
	@RequestMapping(value="/list")
	@CrossOrigin
	public Response<List<Notices>> list(@RequestBody Notices notices, @RequestParam Integer currentPage,
			HttpServletRequest req) throws Exception {
		int pageSize = Integer.parseInt(req.getParameter("pageSize")); //每页显示记录数
		int offset = (currentPage - 1) * pageSize; //当前页开始记录
		int counts = 0; //总记录数
		PageBean page = new PageBean(offset, pageSize); //分页对象
		//查询记录总数
		counts = noticesService.getCount(notices);
		//获取当前页记录
		List<Notices> noticesList = noticesService.queryNoticesList(notices, page);
		//遍历
		for (Notices notices2 : noticesList) {
			notices2.setPldetail(removeHTML.Html2Text(notices2.getPldetail()));

		}

		int page_count = counts % PageBean.PAGE_IETM == 0 ? counts / PageBean.PAGE_IETM
				: counts / PageBean.PAGE_IETM + 1; //总页数
		return Response.success(noticesList, counts, page_count);
	}
	
	// 资讯列表
	@RequestMapping(value = "/list2")
	@CrossOrigin
	public Response<List<Notices>> list2( Notices notices, @RequestParam Integer currentPage,
			HttpServletRequest req) throws Exception {
		int pageSize = Integer.parseInt(req.getParameter("pageSize")); // 每页显示记录数
		int offset = (currentPage - 1) * pageSize; // 当前页开始记录
		int counts = 0; // 总记录数
		PageBean page = new PageBean(offset, pageSize); // 分页对象
		// 查询记录总数
		counts = noticesService.getCount(notices);
		// 获取当前页记录
		List<Notices> noticesList = noticesService.queryNoticesList(notices, page);
		// 遍历
		for (Notices notices2 : noticesList) {
			notices2.setPldetail(removeHTML.Html2Text(notices2.getPldetail()));

		}

		int page_count = counts % PageBean.PAGE_IETM == 0 ? counts / PageBean.PAGE_IETM
				: counts / PageBean.PAGE_IETM + 1; // 总页数
		return Response.success(noticesList, counts, page_count);
	}
        
	//添加资讯
	@ResponseBody
	@PostMapping(value = "/add")
	@CrossOrigin
	public Response add(@RequestBody Notices notices, HttpServletRequest req) throws Exception {
		try {
			noticesService.insertNotices(notices); //添加
   
			} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}
    
	//删除资讯
	@ResponseBody
	@PostMapping(value = "/del")
	@CrossOrigin
	public Response del(HttpServletRequest req) throws Exception {
		try {
			int id = Integer.parseInt(req.getParameter("id"));
			noticesService.deleteNotices(id); //删除
			} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}
	
	//修改资讯
	@ResponseBody
	@PostMapping(value = "/update")
	@CrossOrigin
	public Response update(@RequestBody Notices notices, HttpServletRequest req) throws Exception {
		try {
			noticesService.updateNotices(notices); //修改
			} catch (Exception e) {
			return Response.error();
		}
		return Response.success();
	}
	
	//返回资讯详情
	@ResponseBody
	@PostMapping(value = "/get")
	@CrossOrigin
	public Response get(HttpServletRequest req) throws Exception {
		try {
			int id = Integer.parseInt(req.getParameter("id"));
			Notices notices=noticesService.queryNoticesById(id); //根据ID查询
			return Response.success(notices);
			} catch (Exception e) {
			return Response.error();
		}
       
	}
    
}

